package com.futuristik.objects;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.World;
import com.futuristik.arkitek.Character;

public abstract class ArkitekObject {
	protected float x, y, angle;
	protected Vector2 ray;
	//protected float width;
	//protected float height;
	public Body objectBody;
	
	public float width = 0;
	public float height = 0;
	
	public ArkitekObject(float x, float y){
		this(x, y, 0);
	}
	
	public ArkitekObject(float x, float y, float angle){
		this.x = x;
		this.y = y;
		this.angle = (float) (angle);
		ray = new Vector2((float) (-1*Math.sin(angle)), (float) Math.cos(angle));
	}
	
	public void create(World world){}
	
	public void update(){}
	
	public void render(SpriteBatch batch){}
	
	public boolean prePlayerContact(Character player, Contact contact, Manifold oldManifold){ return false; }
	
	public void playerContact(Character player, Contact cont){
		Vector2 normal = cont.getWorldManifold().getNormal(); //This gives the normal vector of the collision
		
		//Rounding is necessary because the normal vector isn't exact as shown in some tests. :C sad day
		if(round(normal.y) == round(ray.y) && round(normal.x) == round(ray.x)){ //touches the UP part relative to the object's orientation
			this.topContact(player);
		}		
	}
	
	public void topContact(Character player){}

	public static float round(float a){
		return round(a, 6);
	}
	
	public static float round(float a, int precision){
		float temp = (int) Math.pow(10, precision);
		return Math.round(a*temp)/temp;
	}
	public String toString(){
		return this.getClass().getSimpleName()+" "+x+" "+y+" "+angle;
	}
	
	//GETTERS
	public Body getBody(){
		return objectBody;
	}
	
	
	//SETTERS
	public void setPos(float x, float y) {
		this.x = x;
		this.y = y;
	}
	
	public void setAngle(float angle) {
		this.angle = angle;
		ray = new Vector2((float) (-1*Math.sin(angle)), (float) Math.cos(angle));
	}
}
